package com.dmagic.cms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dmagic.cms.common.annotation.FormModel;
import com.dmagic.cms.common.annotation.RequestJsonParam;
import com.dmagic.cms.common.controller.CommonController;
import com.dmagic.cms.common.query.ConditionsBean;
import com.dmagic.cms.common.query.PageUtil;
import com.dmagic.cms.common.util.JsonUtil;
import com.dmagic.cms.common.util.NumberUtil;
import com.dmagic.cms.common.util.StringUtil;
import com.dmagic.cms.entity.Company;
import com.dmagic.cms.entity.Company.Flag;
import com.dmagic.cms.entity.Language;
import com.dmagic.cms.entity.Language.Lang;
import com.dmagic.cms.service.BaseService;
import com.dmagic.cms.service.CompanyService;

@Controller
@RequestMapping(value = "/company")
@SessionAttributes("company_contact")
public class CompanyController extends CommonController {

	private String CONTACTUS_LIST = "company/contactUsList";

	private String ABOUTUS_LIST = "company/aboutUsList";

	private String CONTACTUS_ADD = "company/contactUsAdd";

	private String ABOUTUS_ADD = "company/aboutUsAdd";
	static {
	}

	@Autowired
	private BaseService baseService;

	@Autowired
	private CompanyService companyService;

	@RequestMapping("contactUs")
	public String contactUslist(Model model) {
		model.addAttribute("language", baseService.getAllLanguages());
		model.addAttribute("flag", Flag.ContactUs.getValue());
		return CONTACTUS_LIST;
	}

	@RequestMapping("aboutUs")
	public String aboutUslist(Model model) {
		model.addAttribute("language", baseService.getAllLanguages());
		model.addAttribute("flag", Flag.AboutUs.getValue());
		return ABOUTUS_LIST;
	}

	@RequestMapping(value = "search", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String search(@RequestParam Integer flag, @RequestParam Integer pageNo,
			@RequestParam Integer pageSize, @RequestParam String orderType, @RequestParam String orderBy,
			@RequestJsonParam("rules") List<ConditionsBean> conditions) throws Exception {
		PageUtil<Map> pageUtil = new PageUtil<Map>();
		pageUtil.setAutoCount(true);
		pageUtil.setPageNo(pageNo);
		pageUtil.setPageSize(pageSize);
		pageUtil.setOrder(orderType);
		pageUtil.setOrderBy(orderBy);
		pageUtil.setConditions(conditions);

		if (Flag.ContactUs.compare(flag)) {
			companyService.searchContactUs(pageUtil);
		} else if (Flag.AboutUs.compare(flag)) {
			companyService.searchAboutUs(pageUtil);
		}
		return JsonUtil.toFullJsonWithDateFormat(pageUtil, DATE_FORMAT);
	}

	@RequestMapping("edit/{companyNo}")
	public String edit(@PathVariable("companyNo") String companyNo,
			@RequestParam("languageId") Long languageId, @RequestParam("flag") Integer flag, Model model) {
		List<Company> company = companyService.getCompanyByCompanyNo(companyNo);
		List<Language> languages = baseService.getAllLanguages();
		lang: for (Language l : languages) {
			String languageKey = l.getLanguageKey();
			if (NumberUtil.equals(languageId, l.getLanguageId())) {
				model.addAttribute("defaultForm", languageKey);
			}
			for (Company n : company) {
				if (l.getLanguageId() == n.getLanguageId()) {
					model.addAttribute(languageKey, n);
					continue lang;
				}
			}
		}
		model.addAttribute("language", languages);
		model.addAttribute("flag", flag);
		if (Flag.ContactUs.compare(flag)) {
			return CONTACTUS_ADD;
		} else if (Flag.AboutUs.compare(flag)) {
			return ABOUTUS_ADD;
		}
		return "redirect:/";
	}

	@ModelAttribute("preCompany")
	public Company getCompany(Long companyId) {
		if (companyId == null) {
			return new Company();
		}
		return companyService.getCompany(companyId);
	}

	@RequestMapping("add")
	public String add(@RequestParam Integer flag, Model model) {
		Company company = new Company();
		company.setCompanyNo(getDateFormatNo());
		model.addAttribute("defaultForm", Lang.zh_HK.getValue());
		model.addAttribute(Lang.zh_HK.getValue(), company);
		model.addAttribute(Lang.en.getValue(), company);
		model.addAttribute("language", baseService.getAllLanguages());
		model.addAttribute("flag", flag);
		if (Flag.ContactUs.compare(flag)) {
			return CONTACTUS_ADD;
		} else if (Flag.AboutUs.compare(flag)) {
			return ABOUTUS_ADD;
		}
		return "redirect:/";
	}

	@RequestMapping("save")
	public String save(@RequestParam Integer flag, @FormModel("zh") Company zhCompany,
			@FormModel("en") Company enCompany, RedirectAttributes redirectAttributes) {
		companyService.saveCompany(new Company[] { zhCompany, enCompany });
		redirectAttributes.addFlashAttribute(SUCCESS, "Save Successful.");
		if (Flag.ContactUs.compare(flag)) {
			return "redirect:/company/contactUs.do";
		} else if (Flag.AboutUs.compare(flag)) {
			return "redirect:/company/aboutUs.do";
		}
		return "redirect:/";
	}

	@RequestMapping("delete")
	@ResponseBody
	public String delete(@RequestJsonParam("companyIds") List<Long> companyIds) {
		companyService.deleteCompany(companyIds);
		return "Delete Successful.";
	}

	@RequestMapping("delete/{companyId}")
	@ResponseBody
	public String delete(@PathVariable("companyId") Long companyId) {
		companyService.deleteCompany(companyId);
		return "Delete Successful.";
	}

	@RequestMapping("web/getAboutUs")
	public String getAboutUs(@RequestParam("languageId") Long languageId, @RequestParam("flag") Integer flag, Model model) {
		List<Company> company = companyService.getCompanyPropertyName("flag", flag);
		model.addAttribute("language", languageId);
		List<Company> company_contact = new ArrayList();
		if (Flag.ContactUs.compare(flag)) {
			for (Company c : company) {
				if(NumberUtil.equals(languageId, c.getLanguageId())) {
					company_contact.add(c);
				}
			}
			model.addAttribute("company_contact", company_contact);
			return "web/contact/contact";
		} else if (Flag.AboutUs.compare(flag)) {
			for (Company c : company) {
				if (NumberUtil.equals(languageId, c.getLanguageId())) {
					model.addAttribute("company", c);
					break;
				}
			}
			return "web/about/about";
		}
		return "redirect:/";
	}
	
	@InitBinder
	public void initBinder(WebDataBinder b) {
	}
}
